package com.dgshanger.sanhxiaofeisc.mvp.presenter;

import android.text.TextUtils;

import com.dgshanger.sanhxiaofeisc.App;
import com.dgshanger.sanhxiaofeisc.R;
import com.dgshanger.sanhxiaofeisc.entity.HistoryPaidEntity;
import com.dgshanger.sanhxiaofeisc.entity.UserEntity;
import com.dgshanger.sanhxiaofeisc.mvp.contract.PayForContract;
import com.dgshanger.sanhxiaofeisc.mvp.model.PayForModel;
import com.xx.baseuilibrary.mvp.BaseRxMvpPresenter;
import com.xx.baseutilslibrary.network.rx.RxHttpObserver;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * PayForPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/5/7 17:24.
 */

public class PayForPresenter extends BaseRxMvpPresenter<PayForContract.Model, PayForContract.View> implements PayForContract.Presenter {
    @NotNull
    @Override
    protected PayForContract.Model createModel() {
        return new PayForModel();
    }

    @Override
    public void loadHistory() {
        getModel().loadHistory(new RxHttpObserver<List<HistoryPaidEntity>>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable List<HistoryPaidEntity> entity) {
                if (getView() != null) {
                    getView().setHistoryData(entity);
                }
            }

            @Override
            public void onError(@Nullable String error) {

            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
            }

            @Override
            public void onLoadingFinish() {

            }
        });
    }

    @Override
    public void loadUser(String mobile) {
        getModel().loadUser(mobile, new RxHttpObserver<UserEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable UserEntity entity) {
                if (getView() == null) {
                    return;
                }
                getView().setUserData(entity);
            }

            @Override
            public void onError(@Nullable String error) {

            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
            }

            @Override
            public void onLoadingFinish() {

            }
        });
    }

    @Override
    public void otherPayApply() {
        if (getView() == null) {
            return;
        }
        String otherPayMobile = getView().getOtherPayMobile();
        String otherpayVipId = getView().getOtherpayVipId();
        String otherPayVipQty = getView().getOtherPayVipQty();
        String remark = getView().getRemark();
        if (TextUtils.isEmpty(otherPayMobile)) {
            getView().showToast(App.Companion.getApplication().getString(R.string.Please_enter_the_recipient_account_number));
            return;
        }
        if (otherPayMobile.length() != 11) {
            getView().showToast("请输入正确的账户");
            return;
        }
        getModel().otherPayApply(otherPayMobile, otherpayVipId, otherPayVipQty, remark, new RxHttpObserver<Object>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable Object entity) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(msg);
                getView().onOtherPaySuccess("1");
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(error);
            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog();
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }

    @Override
    public void shopOtherPayApply(String otherpay_shop_id, String otherpay_amount) {
        String otherPayMobile = getView().getOtherPayMobile();
        if (TextUtils.isEmpty(otherPayMobile)) {
            getView().showToast(App.Companion.getApplication().getString(R.string.Please_enter_the_recipient_account_number));
            return;
        }
        getModel().shopOtherPayApply(otherPayMobile, otherpay_shop_id, otherpay_amount, new RxHttpObserver<Object>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable Object entity) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(msg);
                getView().onOtherPaySuccess("2");
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(error);

            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog();
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }
}
